
*=============================================================================*
	capture log close
	clear
	set more off
	
	global wealthdon /* SET FILE PATH */
	global outputfilename "wdon1_master1.dta"

	global raw 				 ${wealthdon}/data/raw
	global projectdatafolder ${wealthdon}/data/projectdata
*=============================================================================*

disp "[$S_DATE $S_TIME] Starting"
*=============================================================================*
*  Open data
*=============================================================================*

* Innt
use ${raw}/innt2010.dta, clear
duplicates drop
gen int _year = 2010
foreach Y of numlist 2011(1)2017 {
	disp "---> Appending innt`Y'.dta"
	append using ${raw}/innt`Y'.dta
	replace _year = `Y' if missing(_year)
	
	if `Y'==2012 {
		duplicates drop
	}
}
sort LOPNR _year
order LOPNR  famlopnr snr hushlopenr regstat_famlopnr _year Year, first
ren _year year
drop Year
drop if missing(LOPNR)

append using ${raw}/inntvar_g2018.dta
replace year = 2018 if missing(year)

* wealth taxes paid (skatt_formue)
desc using ${raw}/formueskatt_g2010g2018.dta
ren hushlopenr hushlopenr_innt
gen aargang = string(year)
merge 1:1 LOPNR aargang using ${raw}/formueskatt_g2010g2018.dta
ren hushlopenr hushlopenr_formueskatt
drop _merge

* Net wealth
desc using ${raw}/formue_g2010g2018.dta
merge 1:1 LOPNR aargang using ${raw}/formue_g2010g2018.dta
drop _merge
ren hushlopenr hushlopenr_formue

replace year = real(aargang) if missing(year)


drop if real(LOPNR)<01000000000 | missing(LOPNR)

* Taxable net wealth: bel49
ren aargang aargang_str
ren year aargang

desc using ${raw}/nettoformue_g2010g2018.dta
merge 1:1 LOPNR aargang using ${raw}/nettoformue_g2010g2018.dta
tab aargang _merge
drop if _merge==2
drop _merge

* Tax values of primary, secondary housing and cabin
desc using ${raw}/bolig_g2010g2018.dta
merge 1:1 LOPNR aargang using ${raw}/bolig_g2010g2018.dta
drop if _merge==2
drop _merge

ren aargang year
ren aargang_str aargang
*=============================================================================*
* Prep gift data
*=============================================================================*


*

merge 1:1 LOPNR year using ${projectdatafolder}/wdon1_donations_person_year.dta
tab year _merge
drop _merge

*=============================================================================*
* Subsets
*=============================================================================*
* Drop persons for which we never see:
*	wealth taxes, age
sort LOPNR year
by LOPNR (year): egen _tot_nm_skatt_formue = total(!missing(skatt_formue))
by LOPNR (year): egen _tot_nm_alder = total(!missing(alder))
drop if _tot_nm_skatt_formue==0
drop if _tot_nm_alder==0
drop _tot*

*=============================================================================*
* Some variable consolidation
*=============================================================================*
* 


replace statsbor = statsborgerskap if year == 2017
drop statsborgerskap
drop pi_ff_famb /* always missing*/
replace pgiv_int = pgiv_innt if year == 2017
drop pgiv_innt


replace famlopnr = regstat_famlopnr if missing(famlopnr) & !missing(regstat_famlopnr)
drop regstat_famlopnr

foreach var of varlist lignkom lign_kom lignkom_sa lignkom_sl {
	replace lignkomm = `var' if missing(lignkomm) & !missing(`var')
	drop `var'
}

replace bokomm = bokomm_2013 if missing(bokomm) & !missing(bokomm_2013)
drop bokomm_2013

replace bokomm = kommnr if missing(bokomm) & !missing(kommnr)
drop kommnr

replace kjonn = kjoenn if missing(kjonn)
drop kjoenn

replace negoverf = neg_overf if missing(negoverf)
drop neg_overf

replace rentinnt = renteinnt if missing(rentinnt)
drop renteinnt

replace rentut = renteutgift if missing(rentut)
drop renteutgift

replace wkapinnt = kapinnt if missing(wkapinnt)
drop kapinnt

replace wsaminnt = saminnt if missing(wsaminnt)
drop saminnt

replace tap = realisasjonstap if missing(tap)
drop realisasjonstap

replace gevinst = realisasjonsgevinst if missing(gevinst)
drop realisasjonsgevinst

destring sivilstand,replace
destring sivstand, replace
replace sivstand = sivilstand if missing(sivstand) & !missing(sivilstand)
drop sivilstand
*=============================================================================*
* Destring
*=============================================================================*
destring LOPNR famlopnr snr hushlopenr_innt statsbor sivstand	///
	 regstat_famtyp fodeland nivafelt utdigang ///
	kjonn hushlopenr_formueskatt hushlopenr_formue, replace

format LOPNR famlopnr hushlopenr_innt hushlopenr_formueskatt hushlopenr_formue %13.0g



*=============================================================================*
* Household stuff and cleaning
*=============================================================================*
drop if alder<17

* Create household ID
gen double hhid = hushlopenr_formueskatt
format hhid %14.0g
drop if missing(hhid)
sort hhid year

* Only keep "regular" households with either 0 or 2 people who are married
by hhid year: egen hhid_year_tot_married = total(sivstand==2), missing
keep if (inlist(hhid_year_tot_married,0,2) & !missing(sivstand) ) 

* those not married and in the sample, gets their own hhid
gen double hhid2 = hhid
replace hhid2 = 100*LOPNR+1 if sivstand!=2 & alder>=17
format hhid2 %15.0g
order hhid2, first

sort hhid2 year

drop hhid
ren hhid2 hhid

tab year 

destring lignkomm, replace
ren lignkomm muni
merge m:1 muni year using ${projectdatafolder}/muni_lignverdi_proptax.dta 
ren muni lignkomm
drop if _merge==2
drop _merge


*=============================================================================*
* Save
*=============================================================================*

compress

save ${projectdatafolder}/${outputfilename}, replace

disp "[$S_DATE $S_TIME] Finished"
